package com.bluemson.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class PictureUtil {
    public static String getUri(MultipartFile picture, HttpServletRequest request) throws IOException {
        String part = picture.getOriginalFilename();
        String substring = part.substring(part.lastIndexOf("."));
        String fileName = UUID.randomUUID()+substring;
        String upload = request.getSession().getServletContext().getRealPath("upload");
        File fileDisk = new File(upload);
        if (!fileDisk.exists()){
            fileDisk.mkdir();
        }
        File uploadFile = new File(upload, fileName);
        picture.transferTo(uploadFile);
        return request.getContextPath() + "/upload/" +fileName;
    }
}
